package com.mjing.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author ming
 */
@Slf4j
public class TokenUtils {

    public static final String TOKEN_KEY_NUM = "_TOKEN_NUM";
    public static final String TOKEN_HEAD = "TOKEN_HEAD";

    /**
     * 生成token值
     * @return
     */
    public static String createToken(String tokenHead) {

        return tokenHead + DigestUtils.md5DigestAsHex(StringUtils.getUUID().getBytes());
    }

    /**
     * 生成token值
     * @return
     */
    public static String createToken(HttpServletRequest request) {

        return TOKEN_HEAD + DigestUtils.md5DigestAsHex(StringUtils.getUUID().getBytes());
    }


    /**
     * 验证token是否正确
     *
     * @return
     */
    public static boolean authToken(String firstToken, String secondToken) {
        if (StringUtils.isNotBlank(firstToken)) {
            return false;
        }
        if (StringUtils.equals(firstToken, secondToken)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        log.info(createToken("authtoken"));
    }

}
